package ${config.controllerConfig.packageUrl};

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import javax.validation.Valid;
import org.springframework.validation.BindingResult;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.klsoft.component.common.globality.constant.SystemConStant;
import com.klsoft.modular.common.controller.BaseController;
import com.klsoft.modular.common.response.ResponseBase;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang.StringUtils;
import ${config.modelConfig.packageUrl}.${table.modelName};
import ${config.serviceConfig.packageUrl}.${config.serviceConfig.filePreFixName}${table.modelName}${config.serviceConfig.fileSuffixName};
import ${config.boConfig.packageUrl}.${table.modelName}${config.boConfig.fileSuffixName};

/**
 * ${table.tableComment}控制器
 * @author ${config.author}
 * @date ${nowDate}
 * @(C) ${config.copyRight} Copyright 
 */
@Controller
public class ${table.modelName}${config.controllerConfig.fileSuffixName} extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(${table.modelName}${config.controllerConfig.fileSuffixName}.class);
	
	@Autowired
	private ${config.serviceConfig.filePreFixName}${table.modelName}${config.serviceConfig.fileSuffixName} ${modelNameVariable}${config.serviceConfig.fileSuffixName};
	
	/** ${table.tableComment} view */
	private static String path${table.modelName}Root="/${modelNameVariable}/";
	
	
	/**
	 * ${table.tableComment}列表页面
	 */
	@RequestMapping(value = "/${modelNameVariable}/${modelNameVariable}List", method = RequestMethod.GET)
	@RequiresPermissions("${modelNameVariable}_man:view")
	public String queryList(HttpServletRequest request) {
		logger.info("${table.tableComment}列表页面");
		return path${table.modelName}Root + "${modelNameVariable}List.ftl";
	}

	/**
	 * ${table.tableComment}列表数据
	 */
	@RequestMapping(value = "/${modelNameVariable}/${modelNameVariable}AjaxList", method = RequestMethod.POST)
	@RequiresPermissions("${modelNameVariable}_man:view")
	public String queryAjaxList(${table.modelName}${config.boConfig.fileSuffixName} ${modelNameVariable}${config.boConfig.fileSuffixName},Page<${table.modelName}> page, HttpServletRequest request) {
		logger.info("${modelNameVariable}列表数据>>data:" + JSON.toJSONString(${modelNameVariable}${config.boConfig.fileSuffixName}));
		page = ${modelNameVariable}${config.serviceConfig.fileSuffixName}.selectPageByBo(page, ${modelNameVariable}${config.boConfig.fileSuffixName});
		request.setAttribute("pager", page);
		return path${table.modelName}Root + "${modelNameVariable}AjaxList.ftl";
	}
	
	
	/**
	 * ${table.tableComment}添加页面
	 * @return
	 */
	@RequestMapping(value="/${modelNameVariable}/to${table.modelName}Add" , method = RequestMethod.GET)
	@RequiresPermissions("${modelNameVariable}_man:add")
	public String toAdd${table.modelName}(){
		logger.info("${table.tableComment}添加页面");
		return path${table.modelName}Root + "add${table.modelName}.ftl";
	}
	
	/**
	 * 添加${table.tableComment}
	 */
	@RequestMapping(value="/${modelNameVariable}/add${table.modelName}" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("${modelNameVariable}_man:add")
	public String add${table.modelName}(@Valid ${table.modelName} ${modelNameVariable}, BindingResult result, HttpServletResponse response) throws Exception{
		logger.info("添加${table.tableComment}>>data:" + JSON.toJSONString(${modelNameVariable}));

		if (result.hasErrors()) {
			return ResponseBase.errorStr(result.getAllErrors().get(0).getDefaultMessage());
		}

		${modelNameVariable}${config.serviceConfig.fileSuffixName}.insert(${modelNameVariable});

		String jsonstr = ResponseBase.success();
		logger.info("添加${table.tableComment}>>返回>>data:" + jsonstr);
		return jsonstr;
	}

	
	/**
	 * ${table.tableComment}更新页面
	 * @return
	 */
	@RequestMapping(value = "/${modelNameVariable}/toChangeView" , method = RequestMethod.GET)
	@RequiresPermissions("${modelNameVariable}_man:update")
	public String toChange${table.modelName}(Long id, HttpServletRequest request) throws Exception{
		logger.info("${table.tableComment}修改页面>>data:" + id);
		${table.modelName} ${modelNameVariable} = ${modelNameVariable}${config.serviceConfig.fileSuffixName}.selectById(id);
		request.setAttribute("${modelNameVariable}", ${modelNameVariable});
		return path${table.modelName}Root + "${modelNameVariable}UpChange.ftl";
	}

	
	/**	
	 * 修改
	 */
	@RequestMapping(value = "/${modelNameVariable}/upChange" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("${modelNameVariable}_man:update")
	public String upChange${table.modelName}(@Valid ${table.modelName} ${modelNameVariable}, BindingResult result, HttpServletResponse response) throws Exception{
		logger.info("更新${table.tableComment}信息>>data:" + JSON.toJSONString(${modelNameVariable}));

		if (result.hasErrors()) {
			return ResponseBase.errorStr(result.getAllErrors().get(0).getDefaultMessage());
		}
		if(${modelNameVariable}.getId() == null){
			return ResponseBase.errorStr(ResponseBase.RESPOSNE_NOT_VALID_ERROR);
		}

		${modelNameVariable}${config.serviceConfig.fileSuffixName}.updateById(${modelNameVariable});

		String jsonstr = ResponseBase.success();
		logger.info("编辑${table.tableComment}信息>>返回>>data:" + jsonstr);
		return jsonstr;
	}

	/**
	 * 批量删除${table.tableComment}
	 */
	@RequestMapping(value = "/${modelNameVariable}/del${table.modelName}" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("${modelNameVariable}_man:delete")
	public String del${table.modelName}(String id,HttpServletResponse response) throws Exception{
		logger.info("批量删除${table.tableComment}>>data:" + JSON.toJSONString(id));

		// 数据验证
		if (StringUtils.isEmpty(id)) {
			return ResponseBase.errorStr(ResponseBase.RESPOSNE_NOT_VALID_ERROR);
		}

		String[] codestrs = id.split(",");

		${modelNameVariable}${config.serviceConfig.fileSuffixName}.logicalDeleteBatch(codestrs);

		String jsonstr = ResponseBase.success();
		logger.info("删除${table.tableComment}资源>>返回view>>data:" + jsonstr);
		return jsonstr;
	}
}
